package com.emergency.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emergency.data.entity.AddressBook;
import com.emergency.data.vo.AddressBookVO;
import com.emergency.mapper.AddressBookMapper;
import com.emergency.service.AddressBookService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (AddressBook)通讯录服务实现类
 *
 * @author makejava
 * @since 2020-09-21
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {

    @Resource
    private AddressBookMapper addressBookMapper;

    @Override
    public Map<String,Object> getAddressBookList(Integer page, Integer size, String name, Integer deptId, Integer streetId, Integer villageId, Integer type) {
        Map<String,Object> map = new HashMap<>();
        List<AddressBookVO> records;
        if(page == null) {
            records = addressBookMapper.getAllAddressBookList(name, deptId, streetId, villageId);
        } else {
            IPage<AddressBookVO> addressBookVOIPage = addressBookMapper.getAddressBookList(new Page<>(page, size), name, deptId, streetId, villageId);
            records = addressBookVOIPage.getRecords();
            long total = addressBookVOIPage.getTotal();
            map.put("total",total);
        }

        if (type != null) {
            records = records.stream()
                    .filter(c -> type == 1 ? StrUtil.isEmpty(c.getVillageName()) : StrUtil.isNotEmpty(c.getVillageName()))
                    .collect(Collectors.toList());
        }
        map.put("addressBookList", records);
        return map;
    }
}
